//
//  AppDelegate.swift
//  mySwiftWeibo
//
//  Created by apple on 16/7/18.
//  Copyright © 2016年 itcast沈庆伟. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    //V1.0AppStore上线,修复,V1.1上线
    //V2.0AppStore上线,留下致命bug
    
    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //设置导航栏皮肤
        self.setAppearance()
        
        //窗口
        window=UIWindow(frame: UIScreen.mainScreen().bounds)
        
        let tabBarVC=BSMainTabBarController()
        tabBarVC.view.backgroundColor=UIColor.whiteColor()
        //窗口根控制器
        //window?.rootViewController=tabBarVC
        //测试
        //window?.rootViewController=BSWelcomeViewController()
        //window?.rootViewController=BSNewFeatureViewController()
        
        //print("isNewVersion: \(isNewVersion())")
        
        window?.rootViewController=defaultViewController()
        
        //主窗口可见
        window?.makeKeyAndVisible()
        
        print("userAccount: \(BSUserAccountViewModel.sharedUserAccountViewModel.userAccount)")

        return true
    }
    
    ///控制器界面跳转-类方法
    class func switchRootViewController(controller: UIViewController){
        //拿到同一个appDelegate对象
        let appDelegate=UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController=controller
    }
    
    ///根据条件进入某个控制器
    private func defaultViewController() -> UIViewController{
        //判断是否登录
        if !BSUserAccountViewModel.sharedUserAccountViewModel.userLogin{
            //没有登录
            return BSMainTabBarController()
        }else{
            //登录了
            let newVersion=isNewVersion()
            return newVersion ? BSNewFeatureViewController() : BSWelcomeViewController()
        }
    }
    
    ///判断是否是新版本
    private func isNewVersion() -> Bool{
        let currentVersion=NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        let key="lastVersion"
        let lastVersion = NSUserDefaults.standardUserDefaults().stringForKey(key)
        let isNewVersion=currentVersion != lastVersion
        
        //比较
        if isNewVersion{
            //新
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: key)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        return isNewVersion
    }
    
    //MARK: - 设置导航栏皮肤
    private func setAppearance(){
        let naviBar=UINavigationBar.appearance()
        naviBar.tintColor=UIColor.orangeColor()
    }
    
    
    
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

